package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.domain.Resume;
import com.woniu.mapper.PositionMapper;
import com.woniu.mapper.ResumeMapper;
import com.woniu.service.ResumeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.vo.PageVo;
import com.woniu.vo.ResumeHotSort;
import org.springframework.data.redis.core.StringRedisTemplate;
import com.woniu.vo.PositionVo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyy
 * @since 2021-03-08
 */
@Service
public class ResumeServiceImpl extends ServiceImpl<ResumeMapper, Resume> implements ResumeService {
    @Resource
    private PositionMapper positionMapper;

    @Resource
    private RedisTemplate<Object, Object> redisTemplate;
    @Resource
    private ResumeMapper resumeMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<PositionVo> findallPosition() {

//求职者查询所有展示职位
        List<PositionVo> positions = (List<PositionVo>) redisTemplate.opsForValue().get("posi");
        if (null == positions) {//从数据库中获取
            System.out.println("mysql");
            positions = positionMapper.findallPosition();
        } else {//从redis中获取
            System.out.println("redis");
        }
        return positions;
    }


    @Override
    public List<Resume> getHotPositions() {

        //从数据库中查询热点信息
        QueryWrapper<Resume> ew = new QueryWrapper<>();
        ew.eq("resumestatus", "公开");
        List<Resume> positions = resumeMapper.selectList(ew);

        if (!ObjectUtils.isEmpty(positions)) {
            positions.forEach(record -> {
                //往redis中新增一些数据
                System.out.println(record);
                stringRedisTemplate.opsForHash().put("resumes1:id:" + record.getResumeId(), "resumes1VisitCount", record.getVisitCount().toString());
                stringRedisTemplate.opsForHash().put("resumes1:id:" + record.getResumeId(), "resumes1Hot", record.getHot());
                //用来做排行榜的
                stringRedisTemplate.opsForSet().add("resumes1:ids", record.getResumeId().toString());
            });
            //全部为发布职位的id
            Set<String> ids = stringRedisTemplate.opsForSet().members("resumes1:ids");
            //用来做后台管理的比较器
            ArrayList<ResumeHotSort> managerSorts = new ArrayList<>();
            //用来做流浪量管理的比较器
            ArrayList<ResumeHotSort> lookSorts = new ArrayList<>();
            //用来存储发布信息的集合
            ArrayList<Resume> needsPositions = new ArrayList<Resume>();


            //处理平台推送
            ids.forEach(id -> {
                Object position1VisitCount = stringRedisTemplate.opsForHash().get("resumes1:id:" + id, "resumes1VisitCount");
                Object position1Hot = stringRedisTemplate.opsForHash().get("resumes1:id:" + id, "resumes1Hot");
                //处理平台推送的
                if("平台推送".equals(position1Hot.toString())){
                    managerSorts.add(ResumeHotSort.builder().resumeId(Integer.valueOf(id))
                            .status(position1Hot.toString())
                            .visitCount(Integer.valueOf(position1VisitCount.toString()))
                            .build());
                    //可以定时更新数据库
                    Resume entity = new Resume();
                    entity.setResumeId(Integer.valueOf(id));
                    entity.setHot("平台推送");
                    entity.setVisitCount(Integer.valueOf(position1VisitCount.toString()));
                    resumeMapper.updateById(entity);
                }else{
                    //处理非平台推送的
                    lookSorts.add(ResumeHotSort.builder().resumeId(Integer.valueOf(id))
                            .status(position1Hot.toString())
                            .visitCount(Integer.valueOf(position1VisitCount.toString()))
                            .build());
                    //可以定时更新数据库
                    Resume entity = new Resume();
                    entity.setResumeId(Integer.valueOf(id));
                    entity.setHot("浏览量推送");
                    entity.setVisitCount(Integer.valueOf(position1VisitCount.toString()));
                    resumeMapper.updateById(entity);
                }
            });
            Collections.sort(managerSorts);
            Collections.sort(lookSorts);
            List<String> sortIds = stringRedisTemplate.opsForList().range("resumes1:sortId", 0, -1);
            if(!ObjectUtils.isEmpty(sortIds)){
                sortIds.forEach(id->{

                    Resume position = resumeMapper.selectById(id);
                    if(ObjectUtils.isEmpty(position)){
                        stringRedisTemplate.opsForList().remove("resumes1:sortId",1, id);
                    }
                    Object position1Hot = stringRedisTemplate.opsForHash().get("resumes1:id:" + id, "resumes1Hot");
                    if(position1Hot.toString().equals("平台推送")){
                        return ;
                    }
                    stringRedisTemplate.opsForHash().put("resumes1:id:" + id, "resumes1Hot","非热点" );
                });
            }
            stringRedisTemplate.delete("resumes1:sortId");

            int count = 0;
            for (int i = 0; i < managerSorts.size(); i++) {
                if (++count > 5) break;
                needsPositions.add(resumeMapper.selectById(managerSorts.get(i).getResumeId()));
                stringRedisTemplate.opsForList().rightPush("resumes1:sortId", (managerSorts.get(i).getResumeId().toString()));
            }
            for(int i=0;i<lookSorts.size();i++){
                if (++count > 5) break;
                needsPositions.add(resumeMapper.selectById(lookSorts.get(i).getResumeId()));
                stringRedisTemplate.opsForList().rightPush("resumes1:sortId", (lookSorts.get(i).getResumeId().toString()));
            }
            sortIds = stringRedisTemplate.opsForList().range("resumes1:sortId", 0, -1);
            needsPositions.clear();
            sortIds.forEach(id->{
                needsPositions.add(resumeMapper.selectById(id));
            });


            for(int i=0;i<needsPositions.size();i++){
                if(needsPositions.get(i)==null){
                    needsPositions.remove(i);
                }
            }
            return needsPositions;
        }
        return null;
    }

    @Override
    public List<PositionVo> findPosition(String tname) {
        List<PositionVo> positions=positionMapper.findPosition(tname);
        return positions;
    }


}


